/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.common.base;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;

import androidx.appcompat.app.AppCompatActivity;

import com.logos.common.base.operation.ITitleOperation;
import com.logos.common.base.operation.TitleOperation;
import com.logos.common.permissions.PermissionsActivity;
import com.logos.common.permissions.PermissionsChecker;
import com.logos.tool.log.LogUtil;
import com.logos.tool.ActivityManagerUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public abstract class BaseActivity extends AppCompatActivity {

    private static final String TAG = BaseActivity.class.getSimpleName();
    public static final String INTENT_LAYOUT_ID = "intentLayoutId";
    private ITitleOperation mTitleOperation;
    private static final int REQUEST_CODE_PERMISSION = 2016;

    private List<String> mNecessaryPermissions = Arrays.asList(
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
            android.Manifest.permission.ACCESS_FINE_LOCATION,
            android.Manifest.permission.ACCESS_NETWORK_STATE,
            android.Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_PHONE_STATE,
            android.Manifest.permission.CAMERA,
            android.Manifest.permission.SEND_SMS,
            android.Manifest.permission.CALL_PHONE,
            android.Manifest.permission.RECORD_AUDIO);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(null);
        ActivityManagerUtils.getInstance().addActivity(this);
        LogUtil.info(TAG, getClass().getSimpleName() + " onCreate " + savedInstanceState);

        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                int intentLayoutId = bundle.getInt(INTENT_LAYOUT_ID, 0);
                if (intentLayoutId != 0) {
                    mTitleOperation = new TitleOperation(getContext(), intentLayoutId);
                }
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        LogUtil.info(TAG, getClass().getSimpleName() + " onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtil.info(TAG, getClass().getSimpleName() + " onResume");

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onPause() {
        super.onPause();
        LogUtil.info(TAG, getClass().getSimpleName() + " onPause");
    }

    @Override
    protected void onDestroy() {
        ActivityManagerUtils.getInstance().finishActivity(this);
        super.onDestroy();
        LogUtil.info(TAG, getClass().getSimpleName() + " onDestroy");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        LogUtil.info(TAG, getClass().getSimpleName() + " onRestart");
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        return super.dispatchTouchEvent(event);
    }

    protected Context getContext() {
        return this;
    }

    public ITitleOperation getTitleOperation() {

        return mTitleOperation;
    }

    private String[] getPermissions() {
        PackageInfo packageInfo = null;
        String[] requestedPermissions = null;
        List<String> list = new ArrayList<>();
        try {
            packageInfo = getPackageManager().getPackageInfo(getPackageName(),
                    PackageManager.GET_PERMISSIONS);
            requestedPermissions = packageInfo.requestedPermissions;
            for (String permissions : requestedPermissions) {
                for (String necessary : mNecessaryPermissions) {
                    if (permissions.equals(necessary)) {
                        list.add(permissions);
                    }
                }
            }
            return list.toArray(new String[0]);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return mNecessaryPermissions.toArray(new String[0]);
    }

    private void checkerPermission() {
        String packageName = getPackageName();
        LogUtil.info("packageName", "当前应用包名是:" + packageName);

        if (Build.VERSION.SDK_INT >= 23 && new PermissionsChecker(this)
                .lacksPermissions(getPermissions())) {
            PermissionsActivity.startActivityForResult(
                    this, REQUEST_CODE_PERMISSION, getPermissions());
        }
    }
}
